package com.cloudbroker.bcs.platform.ltb.middle.bizservice.impl;

import com.alibaba.fastjson.JSONObject;
import com.cloudbroker.bcs.common.configure.ConfigUtil;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.csvc.lightinvesting.api.t2service.LedSecT2Service;
import com.cloudbroker.bcs.csvc.lightinvesting.api.t2service.LedT2Service;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.cashTransactions.ComuserSignRenewForm;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.fansRelated.ComuserSignCancelForm;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.fansRelated.ComuserSignForm;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.ComuserSignCancelVo;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.ComuserSignRenewVo;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.ComuserSignVo;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.push.vo.TagsAddForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.push.vo.TagsRemoveForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.transaction.ComUserSignBizService;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.transaction.back.DongbeiSignCallbackBack;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.transaction.back.DongbeiSignStatusBack;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.transaction.vo.*;
import com.cloudbroker.bcs.platform.ltb.middle.service.PushService;
import com.thinkive.openservice.api.TKOpenServiceClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by daibd19671 on 2016/10/14.
 */
@Service
public class ComUserSignBizServiceImpl implements ComUserSignBizService{
    @Resource
    private LedT2Service ledT2Service;

    @Resource
    private LedSecT2Service ledSecT2Service;
    @Resource
    private PushService pushService;

    @Override
    public ComuserSignVo comUserSigh(MiddleComuserSignForm form) {
        ComuserSignForm comuserSignForm = new ComuserSignForm();
        comuserSignForm.setUser_type(form.getUser_type());
        comuserSignForm.setCompany_id(form.getCompany_id());
        comuserSignForm.setAdviser_id(form.getAdviser_id());
        comuserSignForm.setBranch_no(form.getBranch_no());
        comuserSignForm.setOp_entrust_way(form.getOp_entrust_way());
        comuserSignForm.setOp_station(form.getOp_station());
        comuserSignForm.setPassword(form.getPassword());
        comuserSignForm.setPassword_type(form.getPassword_type());
        comuserSignForm.setUser_token(form.getUser_token());
        comuserSignForm.setSendercomp_id(form.getSendercomp_id());
        comuserSignForm.setUser_id(String.valueOf(form.getUser().getUser_id()));
        comuserSignForm.setNeed_user_id(form.getNeed_user_id());
        comuserSignForm.setAdviser_id(form.getAdviser_id());
        comuserSignForm.setPay_mode(form.getPay_mode());
        comuserSignForm.setComfare_mode(form.getComfare_mode());
        comuserSignForm.setCompay_channel(form.getCompay_channel());
        comuserSignForm.setMoney_type(form.getMoney_type());
        comuserSignForm.setPrepay_balance(form.getPrepay_balance());
        comuserSignForm.setChannel_id(form.getChannel_id());
        comuserSignForm.setEnd_date(form.getEnd_date());
        comuserSignForm.setPartner_id(ConfigUtil.getString(form.getCompany_id(),"wx.app.partnerId","0"));
        ComuserSignVo combSubscribeVo = ledT2Service.func_comuser_sign(comuserSignForm);
        //判断是否需要支付
        if (VOUtil.isSuccess(combSubscribeVo)
                && (form.getComfare_mode().equals("1") || form.getPrepay_balance() == 0d)
                && form.getCompany_id().equals("20020")) {
            TagsAddForm tagsAddForm = new TagsAddForm();
            tagsAddForm.setCompany_id("20020");
            tagsAddForm.setSendercomp_id("20020");
            tagsAddForm.setUser_id(form.getUser().getUser_id());
            tagsAddForm.setTag(form.getAdviser_id());
            pushService.xinanTagsAdd(tagsAddForm);
        }
//        if (VOUtil.isSuccess(combSubscribeVo) && form.getCompany_id().equals("20020")) {
//            FareargQryForm fareargQryForm = new FareargQryForm();
//            fareargQryForm.setCompany_id("20020");
//            fareargQryForm.setSendercomp_id("20020");
//            fareargQryForm.setAdviser_id(form.getAdviser_id());
//            FareargQryVo fareargQryVo = ledT2Service.func_farearg_qry(fareargQryForm);
//            if(VOUtil.isSuccess(fareargQryVo) && fareargQryVo.getFare_per_month() == 0d){
//                TagsAddForm tagsAddForm = new TagsAddForm();
//                tagsAddForm.setCompany_id("20020");
//                tagsAddForm.setSendercomp_id("20020");
//                tagsAddForm.setUser_id(form.getUser().getUser_id());
//                tagsAddForm.setTag(form.getAdviser_id());
//                pushService.xinanTagsAdd(tagsAddForm);
//            }
//        }
        return combSubscribeVo;

    }


    @Override
    public DongbeiSignStatusBack dongbei_sign_status(DongbeiSignStatusForm form) {
        Map<String,String> param = new HashMap<String,String>(3);
        param.put("khh",form.getBusin_account() );
        param.put("invest_id", String.valueOf(form.getAdviser_id()));
        param.put("source", "04");
        String result = TKOpenServiceClient.sendData("200010", param);
        System.out.println(param.toString());
        System.out.println(result);
        JSONObject resultJson = JSONObject.parseObject(result);
        if(!resultJson.get("error_no").toString().equals("0")){
            VOUtil.genBGErrorResult(resultJson.getString("error_no"),
                    resultJson.getString("error_info"),DongbeiSignStatusBack.class);
        }
        DongbeiSignStatusBack dongbeiSignStatusBack = new DongbeiSignStatusBack();
        dongbeiSignStatusBack.setSign_status(resultJson.getString("sign_status"));
        return VOUtil.setSuccessResult(dongbeiSignStatusBack);
    }


    @Override
    public DongbeiSignCallbackBack dongbei_sign_callback(DongbeiSignCallbackForm form) {
        ComuserSignForm comuserSignForm= new ComuserSignForm();
        BeanUtils.copyProperties(form,comuserSignForm);
        ledT2Service.func_comuser_sign(comuserSignForm);
        return null;
    }

    @Override
    public ComuserSignRenewVo func_comuser_sign_renew(MiddleComUserSignRenewForm param) {
        ComuserSignRenewForm comuserSignRenewForm = new ComuserSignRenewForm();
        param.setPartner_id(ConfigUtil.getString(param.getCompany_id(),"wx.app.partnerId","0"));
        BeanUtils.copyProperties(param,comuserSignRenewForm);
        return ledSecT2Service.func_comuser_sign_renew(comuserSignRenewForm);
    }

    @Override
    public ComuserSignCancelVo func_comuser_sign_cancel(MiddleComuserSignCancelForm form) {
        ComuserSignCancelForm comuserSignCancelForm = new ComuserSignCancelForm();
        BeanUtils.copyProperties(form,comuserSignCancelForm);
        comuserSignCancelForm.setUser_token(form.getUser_token());
        comuserSignCancelForm.setAdviser_id(form.getAdviser_id());
        comuserSignCancelForm.setUser_id(String.valueOf(form.getUser().getUser_id()));
        ComuserSignCancelVo comuserSignCancelVo = ledT2Service.func_comuser_sign_cancel(comuserSignCancelForm);
        if(VOUtil.isSuccess(comuserSignCancelVo)){
            TagsRemoveForm tagsRemoveForm = new TagsRemoveForm();
            tagsRemoveForm.setCompany_id("20020");
            tagsRemoveForm.setSendercomp_id("20020");
            tagsRemoveForm.setUser_id(form.getUser().getUser_id());
            tagsRemoveForm.setTag(form.getAdviser_id());
            pushService.xinanTagsDelete(tagsRemoveForm);
        }
        return comuserSignCancelVo;
    }

}
